# -*- coding:utf-8 -*-
#处理隐形眼镜的数据
import requests
import json
from pymongo import MongoClient
from product.mongodb import mongodb
import re

conn = MongoClient(mongodb.ip, 27017)
db = conn.pinduoduo
goodsCustomDb = db.goodsCustom
# 隐形眼镜typeID

class custom:
    def getData(self,data,goodsTypeId):
        sign_code = "无"
         #判断数据中是否存在goods_property，如果有并存在注册证号，则将map中的value转换为文本
        if('goods_property' in data):
            for temp in data['goods_property']:
                if('key' in temp):
                   if('注册证号' == temp['key'] and 'values' in temp):
                       sign_code = ','.join(list(temp['values']))
        else:
            #第一种方式，正则匹配goods_desc中是否存在'国械注准'字样，如果有，正则匹配并获取文本
            reData = re.search(r'(国械注准)[0-9]+',data['goods_desc'].replace(' ', ''), re.M | re.I)
            if (None != reData):
                sign_code = reData.group()
            else:
                #第二种方式，查看goods_desc中是否有'国食药监械'字样，如果有，匹配'国食药监械'-'号'中间的文本
                if("国食药监械" in data['goods_desc']):
                    goods_desc = data['goods_desc']
                    sign_code = goods_desc[goods_desc.index('国食药监械'):(goods_desc.index('号',goods_desc.index('国食药监械')))+1]
        goodsCustomDb.insert({
             "goods_id": str(data['goods_id']),    #商品ID
             "goods_type_id": goodsTypeId,   #商品类型ID
             "goods_name": data['goods_name'],#商品名称
             "sales": str(data['sales']),   #售卖多少件
             "sign_code":sign_code,
             "link_url":'https://mobile.yangkeduo.com/goods.html?goods_id='+str(data['goods_id'])
        })